/**
##############################################################################
#
# Copyright (C) 2020 Bonainfo <guoyihot@outlook.com>
# All Rights Reserved
#
##############################################################################
 */
odoo.define('iant_dynamic_list_form.DynamicListModel', function(require) {

    "use strict";

	var BasicModel = require('web.BasicModel');
    var session = require('web.session');
    var uid = session.uid;
    var excludeFields = ['id', 'create_date', 'create_uid', 'write_date','write_uid', '__last_update','display_name'];

    return BasicModel.extend({

 	    loadFieldConfig: function(){
 		    var self = this;
 		    var params = {};
 		    var fields = {
				name:{type:'char'},
				string:{type:'char'},
				width:{type:'float'},
				invisible:{type:'boolean'},
				form_field:{type:'char'},
				res_model:{type:'char'},
				act_id:{type:'integer'},
				view_id:{type:'integer'},
				user_id:{type:'integer'},
				sequence:{type:'integer'},
				is_field:{type:'boolean'}
 		    };
 		    params.domain = [['user_id','=',uid],['act_id','=',this._action_id],['view_id','=',this._view_id]];
 		    params.fields = fields;
 		    params.fieldsInfo={list:fields};
 		    params.context = session.user_context;
 		    params.limit = 1000;
 		    params.modelName = 'field.config';
 		    params.viewType = 'list';

 		    return this.load(params).then(function(recordId){
 			    self.fieldConfigId = recordId;
 		    })

 	   },

 	    loadListConfig: function(){
		   var self = this;
		   var params = {};
		   var fields ={
				   			name:{type:'char'},
				   			value:{type:'char'},
				   			form_field:{type:'char'},
				   			act_id:{type:'integer'},
				   			res_model:{type:'char'},
				   			view_id:{type:'integer'},
				   			user_id:{type:'integer'},
		   				};
		  params.domain = [['user_id','=',uid],['act_id','=',this._action_id],['view_id','=',this._view_id]];
		  params.fields = fields;
		  params.fieldsInfo={list:fields};
		  params.context = session.user_context;
		  params.limit = 1000;
		  params.modelName = 'list.config';
		  params.viewType = 'list';

		  return this.load(params).then(function(recordId){
			  self.listConfigId = recordId;
		  })
	   },

	    getSortFieldConfig:function(formField,res_model){
		   var fieldConfigs = [];
		   if(!res_model){
			   return fieldConfigs;
		   }
		   if(this.fieldConfigId){
		   	var configs = this.get(this.fieldConfigId);
		   	if(configs && _.size(configs.data)){
		   		var mapConfigs = _.chain(configs.data).filter(c=>excludeFields.indexOf(c.data.name)===-1).map(function(config){
		   			return {id:config.id,sequence:config.data.sequence,config:config,data:config.data};
		   		}).value();
		   		if(formField){
		   			fieldConfigs = _.chain(mapConfigs).filter(function(field){return field.data.form_field==formField && field.data.res_model==res_model}).sortBy('sequence').map(function(c){return c.config}).value();
		   		}else{
		   			fieldConfigs = _.chain(mapConfigs).filter(function(field){return field.data.res_model==res_model}).sortBy('sequence').map(function(c){return c.config}).value();}
		   	}
   		  }
		   return fieldConfigs;
	   },

	    getListConfig:function(formField,res_model){
		  var configs = [];
		  if(!res_model){
			  return configs
		  }
		  if(this.listConfigId){
			  var record = this.get(this.listConfigId);
			  if(record){
				  if(formField){
					  configs = _.filter(record.data,function(d){return d.data.form_field==formField && d.data.res_model==res_model});
				  }else{
				  configs = _.filter(record.data,function(d){return d.data.res_model==res_model});
				  }
			  }
		  }
		  return configs
	  },

	    _getFieldNamesByConfig:function(formField,res_model){
	    	var fieldNames = [];
	    	if(!res_model){
	    		return fieldNames;
	    	}
	    	var configs = this.get(this.fieldConfigId);
	    	if(configs){
	    		_.each(configs.data,function(config){
	    			if(formField){
	    				if(config.data.res_model==res_model && config.data.form_field == formField && !config.data.invisible && config.data.is_field){
	    					fieldNames.push(config.data.name);
	    				}
	    			}else{
	    				if(config.data.res_model==res_model && !config.data.invisible && config.data.is_field){
	    					fieldNames.push(config.data.name);
	    				}
	    			}
	    		});
	    	}
	    	return fieldNames;
	    },

	    getFieldWidth:function(field,formField,res_model){
	    	var width=-1;
	    	var configs= this.get(this.fieldConfigId);
	    	var widthConfig = _.chain(configs.data).filter(function(config){return config.data.name==field && config.data.res_model==res_model && config.data.form_field==formField}).first().value();
	    	if(widthConfig){
	    		width = widthConfig.data.width;
	    	}
	    	return width;
	    },

	    _getListLimit:function(formField, res_model){
	    	var configs = this.get(this.listConfigId);
	    	var limit = 80;
			var self = this;

	    	if(configs){
	    		var config = _.chain(configs.data).filter(function(f){
	    			if(self.getParent() &&  _.has(self.getParent().pager, 'state')){
	    				return self.getParent().pager.state.limit
					}

	    			if(formField){
	    				return f.data.form_field == formField && f.data.name=='limit';
	    			}

	    			return  f.data.name=='limit' && f.data.res_model==res_model;

	    		}).first().value();
	    		if(config){
	    			var configValue = parseInt(config.data.value);
	    			limit = isNaN(configValue) ? 80 : configValue;
	    		}
	    	}
	    	return limit;

	    }
    
    
    });
     
    
    

  
});
